<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title>物料类别</title>
    <link rel="stylesheet" href="../../../../public/dist/css/layui.css">
</head>

<body>

    <form class="layui-form" action="" id="example" lay-filter="example">
        <div class="select common">
            <div class="select-title"> 物料类别</div>
            <div class="select-con">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">创建时间:</label>
                        <div class="layui-input-inline">
                            <input type="text" id="tesss" class="layui-input input-d   ming" name="createTime" placeholder="输入时间">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">物料类别:</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input input-d  house" placeholder="输入物料类别" name="description">
                        </div>
                    </div>
                </div>


                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">上传图片:</label>
                        <div class="layui-input-inline" id="hether">
                            <img alt="" style="width:100px;height: 40px;" id="picture">
                            <button type="button" class="layui-btn  layui-btn-sm" id="testsd">点击上传</button>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">上层类别:</label>
                        <div class="layui-input-inline ">
                            <select name="parentName" lay-filter="shiss" class="superior">
                                                <option value="">---请选择---</option>
                                                
                                         </select>
                        </div>
                    </div>
                </div>


                <div class="select-btn" style="margin-left:30px;">
                    <div class="layui-bg-green comquery queding" id="queding">确定</div>
                    <div class="layui-bg-green comquery cancels">取消</div>
                </div>
            </div>
        </div>
    </form>
</body>
<script src="../../../../js/module/jquery-3.4.1.min.js"></script>
<script src="../../../../public/dist/layui.js" charset="utf-8"></script>
<script src="../../../../js/common/api.js"></script>
<script type="text/javascript">
    layui.use(["laydate", "form", "upload"], function() {
        var $ = layui.jquery,
            form = layui.form,
            laydate = layui.laydate,
            upload = layui.upload,
            obj = {};

        //时间
        laydate.render({
            elem: '#tesss'
        });

        //取消
        $(".cancels").click(function() {
            window.location.href = "../materialcategory.html";
        });

        obj.parentName = "";

        //上传图片
        upload.render({
            elem: '#testsd',
            url: base + 'common/upload',
            method: "post",
            data: {
                file: "物料类别",
            },
            headers: {
                Authorization: "Bearer" + " " + sessions
            },
            done: function(res) {
                console.log(res)
                $("#picture").attr("src", res.list[0]);
                obj.logoImage = res.list[0];
            },
            error: function() {
                //请求异常回调
            }
        });

        //上级类别
        noAjax("coa/getProductTypeList", {}, "get").done(function(resp) {
            console.log(resp.ipage.records)
            $.each(resp.ipage.records, function(v, item) {
                $(".superior").append('<option  value="' + item.description + '">' + item.description + '</option>')
            })
            form.render('select');
            return
        }).fail(function(err) {
            console.log(err)
        });

        form.on('select(shiss)', function(data) {
            obj.parentName = data.value;
            return
        });

        //编辑
        var urls = window.location.search;
        urls = urls.substring(4);
        console.log(urls)
        noAjax("coa/getProductTypeList", {
            id: urls
        }, "get").done(function(resp) {
            console.log(resp.ipage.records)
            var ss = resp.ipage.records[0].createTime;
            resp.ipage.records[0].createTime = ss.substring(0, 10);
            $("#picture").attr("src", resp.ipage.records[0].logoImage)
            form.val("example", resp.ipage.records[0])
            form.render('select');
            return
        }).fail(function(err) {
            console.log(err)
        });


        //确定
        $("#queding").click(function() {

            if ($("#tesss").val() == "") {
                alerts("请选择时间")
                return
            }
            if ($(".house").val() == "") {
                alerts("请输入物料类别")
                return
            }
            if (obj.logoImage == "") {
                alerts("请选择图片")
                return
            }

            obj.createTime = $("#tesss").val() + " " + "00:00:00";
            obj.description = $(".house").val();

            if (urls != "") {
                obj.id = urls;

                handleAjax("coa/updateProductType", obj, "POST").done(function(resp) {
                    layer.msg('操作成功', {
                        icon: 1,
                        time: 1000
                    });
                    setTimeout(function() {
                        window.location.href = "../materialcategory.html";
                    }, 1500)
                    return
                }).fail(function(err) {
                    console.log(err)
                });

            } else {
                handleAjax("coa/addProductType", obj, "POST").done(function(resp) {
                    layer.msg('操作成功', {
                        icon: 1,
                        time: 1000
                    });
                    setTimeout(function() {
                        window.location.href = "../materialcategory.html";
                    }, 1500)
                    return
                }).fail(function(err) {
                    console.log(err)
                });
            }

        });
    });
</script>

</html>